package org.action.order.sercive.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.action.order.domain.entity.ActionTicketUser;
import org.action.order.domain.entity.ActionUserPay;
import org.action.order.domain.pojo.ActionOrderList;
import org.action.order.mapper.ActionUserPayMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.action.order.sercive.IActionTicketUserService;
import org.museum.api.client.ActionCommonClient;
import org.museum.api.client.ActionInventoryClient;
import org.museum.api.client.IdentityNameClient;
import org.museum.api.domain.Action;
import org.museum.api.util.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.action.order.sercive.IActionUserPayService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zafkiel
 * @since 2025-07-03
 */
@Service
public class ActionUserPayServiceImpl extends ServiceImpl<ActionUserPayMapper, ActionUserPay> implements IActionUserPayService {

    @Autowired
    private ActionCommonClient actionCommonClient;
    @Autowired
    private ActionInventoryClient actionInventoryClient;
    @Autowired
    private IdentityNameClient identityNameClient;
    @Autowired
    private IActionTicketUserService actionTicketUserService;


    @Override
    public List<ActionOrderList> getMyOrder(String userId) {
        List<ActionOrderList> res=new ArrayList<>();
        //获取所有订单
        List<ActionUserPay> list = this.list(new QueryWrapper<ActionUserPay>().eq("user_id", userId));
        //订单为空直接返回
        if(list==null|| list.isEmpty()) return Collections.emptyList();

        for(ActionUserPay pay:list) {
            ActionOrderList orderList = new ActionOrderList();
            orderList.setId(pay.getId());
            //获取订单信息
            ActionTicketUser actionTicketUser = actionTicketUserService.getOne(new QueryWrapper<ActionTicketUser>().eq("id", pay.getOrderId()));
            //获取订单的id
            orderList.setOrderId(pay.getOrderId());
            //获取订单创建时间
            orderList.setCreateTime(actionTicketUser.getCreateTime());
            //获取订单价格
            orderList.setPrice(actionTicketUser.getPrice());

            //根据id查询姓名
            CommonResponse<String> relativeName = identityNameClient.getIdentityName(actionTicketUser.getRelativeIdentity());
            orderList.getUserName().add(relativeName.getData());
            CommonResponse<String> childrenName = identityNameClient.getIdentityName(actionTicketUser.getChildrenIdentity());
            orderList.getUserName().add(childrenName.getData());

            //获取活动信息
            CommonResponse<Integer> actionId = actionInventoryClient.getActionIdById(actionTicketUser.getActionTicketId());
            System.out.println("活动id："+actionId);
            //使用胡图工具包，把转换成实体类

            CommonResponse<Action> response =  actionCommonClient.getActionById(actionId.getData());
            Action action = response.getData();
            System.out.println("活动："+action);
            //获取活动名称
            orderList.setActionName(action.getActionName());
            //获取活动图片
            orderList.setActionCover(action.getActionCover());
            //获取活动时间
            orderList.setActionDate(action.getActionDate());
            //获取订单状态
            orderList.setStatus(pay.getPayStatus());
            res.add(orderList);
        }
        return res;
    }

    @Override
    public boolean getUserOrderStatusById(Long orderId) {
        ActionUserPay pay = this.getOne(new QueryWrapper<ActionUserPay>().eq("order_id", orderId));
        if(pay==null) return false;
        return pay.getPayStatus() == 1;
    }

    @Override
    @Transactional
    public boolean payOrder(Long orderId) {
        //更新对应的订单状态为1
        if(this.update(new ActionUserPay().setPayStatus(1).setPayTime(LocalDateTime.now()),new QueryWrapper<ActionUserPay>().eq("order_id", orderId).eq("pay_status",0))){
            return actionTicketUserService.update(new ActionTicketUser().setStatus(1), new QueryWrapper<ActionTicketUser>().eq("id", orderId).eq("status", 0));
        }
        return false;
    }
}
